package com.zjj.xiancheng.tongxun;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class Account {

    private String name;
    private double balance;

    public Account deposit(double money) {
        synchronized (this){
            String name = Thread.currentThread().getName();
            while (this.balance>0.0){
                System.out.println(name+"正准备存钱，账户余额充足，存款失败，进入等待状态");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            this.balance += money;
            System.out.println(name+"正在存钱，存款后账户余额："+this.balance);
            this.notifyAll();
        }
        return this;
    }

    public Account withdraw(double money) {
        synchronized (this){
            String name = Thread.currentThread().getName();
            while (this.balance<money) {
                System.out.println(name+"准备取款，账户余额不足，取款失败，进入等待中");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            if (this.balance>=money) {
                this.balance -= money;
                System.out.println(name+"正在取款，取款后余额："+this.balance);
                this.notifyAll();
            }
        }
        return this;
    }

    public Account() {
    }

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * 设置
     * @param balance
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String toString() {
        return "Account{name = " + name + ", balance = " + balance + "}";
    }
}
